from typing import Optional

from fastapi import HTTPException

from pydantic import BaseModel, field_validator

from datetime import datetime


class Forecast_to_sql(BaseModel):
    dateTime: datetime
    temperature: float
    weatherCondition: str
    regionId: Optional[int] = None


class Forecast_response(Forecast_to_sql):
    id: int


class Upload_forecast(Forecast_to_sql):
    dateTime: str

    @field_validator('regionId')
    @classmethod
    def validate_regionId(cls, regionId: Optional[int]):
        if regionId is None or regionId <= 0:
            raise HTTPException(status_code=400, detail="bad regionId")
        return regionId

    @field_validator('dateTime')
    @classmethod
    def validate_measurementDateTime(cls, dateTime: str):
        try:
            datetime.fromisoformat(dateTime)
        except Exception:
            raise HTTPException(status_code=400, detail="bad datetime")
        return dateTime


class Update_forecast(Forecast_to_sql):
    dateTime: str

    @field_validator('dateTime')
    @classmethod
    def validate_measurementDateTime(cls, dateTime: str):
        try:
            datetime.fromisoformat(dateTime)
        except Exception:
            raise HTTPException(status_code=400, detail="bad datetime")
        return dateTime


class Forecast(BaseModel):
    id: int
    dateTime: str
    temperature: float
    weatherCondition: str
    regionId: int
